package com.yxkj.syh.app.huarong.activities.orders.detail.status0;

import android.app.Application;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.syh.app.basic.base.BaseResponse;
import com.syh.app.basic.base.view_model.BaseViewModel;
import com.syh.app.basic.bean.LiveDataEvent;
import com.syh.app.basic.bindingAdp.view_click.ClickEvent;
import com.syh.app.basic.config.BasicConstants;
import com.syh.app.basic.utils.Tooast;
import com.syh.app.basic.utils.net.ApiObserver;
import com.trello.rxlifecycle2.android.ActivityEvent;
import com.yxkj.syh.app.huarong.bean.Goods;
import com.yxkj.syh.app.huarong.bean.OrderDetail;
import com.yxkj.syh.app.huarong.bean.OrderDetailResponse;
import com.yxkj.syh.app.huarong.data_center.model.OrderModel;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 创建者：SYH
 * 时间：2020/5/29 13:08
 * 编辑者：SYH、
 * 标签：
 * 补充说明：
 */
public class OrderWaitVM extends BaseViewModel<ActivityEvent> {

    public OrderWaitVM(@NonNull Application application) {
        super(application);
    }

    public MutableLiveData<OrderDetail> mldOrderDetail = new MutableLiveData<>();
    public MutableLiveData<List<Goods>> mldEditGoods = new MutableLiveData<>();

    public ClickEvent cancelClick = new ClickEvent() {
        @Override
        public void onClick(View view) {
            if (mldOrderDetail.getValue() == null) return;
            cancelOrder();
        }
    };

    public void getOrderDetail(long id) {
        OrderModel.getOrderModel().orderDetail(id, mLifecycleProvider.bindToLifecycle(), new ApiObserver<OrderDetailResponse>() {
            @Override
            public void onSuccess(OrderDetailResponse orderDetailResponse) {
                mldOrderDetail.setValue(orderDetailResponse.getData());
            }

            @Override
            public void onFailed(int code, String msg) {
                Tooast.warning(msg);
            }
        });
    }

    public void cancelOrder() {
        Map<String, Object> pMap = new HashMap<>();
        pMap.put("flag", 0);
        pMap.put("id", mldOrderDetail.getValue().getOrderInfo().getId());
        OrderModel.getOrderModel().cancelOrder(pMap, mLifecycleProvider.bindToLifecycle(), new ApiObserver<BaseResponse>() {
            @Override
            public void onSuccess(BaseResponse response) {
                Tooast.success("取消成功");
                EventBus.getDefault().post(new LiveDataEvent(BasicConstants.EVENT_SELECT_ORDER_ALL));
                finish();
            }

            @Override
            public void onFailed(int code, String msg) {
                Tooast.warning(msg);
            }
        });
    }

    public void editGoods(List<Goods> goodsListP){
        Map<String, Object> pMap = new HashMap<>();
        List<Map<String, Object>> goodsList = new ArrayList<>();
        for (Goods goods : goodsListP) {
            Map<String, Object> goodsMap = new HashMap<>();
            goodsMap.put("quantity", goods.getQuantity());
            goodsMap.put("packages", goods.getPackages());
            goodsMap.put("productNo", goods.getProductNo());
            goodsMap.put("productId", goods.getProductId());
            goodsMap.put("name", goods.getName());
            goodsList.add(goodsMap);
        }
        pMap.put("details", goodsList);
        pMap.put("bankId", mldOrderDetail.getValue().getOrderInfo().getBankId());
        pMap.put("id", mldOrderDetail.getValue().getOrderInfo().getId());

        showLoading("editGoodsOrPayment");
        OrderModel.getOrderModel().editGoodsOrPayment(pMap, mLifecycleProvider.bindToLifecycle(), new ApiObserver<BaseResponse>() {
            @Override
            public void onSuccess(BaseResponse response) {
                hideLoading("editGoodsOrPayment");
                mldEditGoods.setValue(goodsListP);
            }

            @Override
            public void onFailed(int code, String msg) {
                hideLoading("editGoodsOrPayment");
                Tooast.warning(msg);
            }
        });
    }

}
